package com.ctl.utils.redis;

import java.util.*;

import redis.clients.jedis.*;

public class RedisShardedJedisPipeline {
	// 分布式直连异步调用
	public static void main(String[] args) {

		List<JedisPool> pools = new ArrayList<>();
		pools.add(new JedisPool(new JedisPoolConfig(), RedisConfig.host, RedisConfig.port));

		String auth = RedisConfig.auth; // 如果有密码验证

		for (JedisPool pool : pools) {
			try (Jedis jedis = pool.getResource()) {
				if (!auth.isEmpty()) {
					jedis.auth(auth);
				}

				long start = System.currentTimeMillis();
				try (Pipeline jedisPipeline = jedis.pipelined()) {
					for (int i = 0; i < 10; i++) {
						jedisPipeline.set("sp" + i, "p" + i);
					}
					List<Object> results = jedisPipeline.syncAndReturnAll();
					long end = System.currentTimeMillis();
					System.out.println("Pipelined SET on pool: "  + " took " + ((end - start) / 1000.0) + " seconds results:" + results.size());
				}
			}
		}

		// 关闭所有连接池
		pools.forEach(JedisPool::close);
	}
}
